package com.bw.service.impl;

import com.bw.dao.GoodsDao;
import com.bw.dao.impl.GoodsDaoImpl;
import com.bw.pojo.Goods;
import com.bw.pojo.vo.GoodsVo;
import com.bw.service.GoodsService;
import com.bw.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
    GoodsDaoImpl dao = new GoodsDaoImpl();

    @Override
    public PageInfo<GoodsVo> getGoodsList(GoodsVo goodsVo) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodsDao goodsMapper = sqlSession.getMapper(GoodsDao.class);
        //开始分页 将分页参数 传递给pageHelper分页查询
        PageHelper.startPage(goodsVo.getPageNum(), goodsVo.getPageSize());
        //执行查询
        List<GoodsVo> list = goodsMapper.getGoodsList(goodsVo);
        PageInfo<GoodsVo> pageInfo = new PageInfo<>(list);
        sqlSession.close();
        return pageInfo;
    }
    @Override
    public void getToDel(String ids) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodsDao mapper = sqlSession.getMapper(GoodsDao.class);
        String[] split = ids.split(",");
        mapper.getToDel(split);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public GoodsVo findById(GoodsVo goodsVo) {
        GoodsVo g =   dao.findById(goodsVo.getGoodsId());
        return g;
    }
    @Override
    public void getGoodsAdd(Goods goods) {
        dao.getGoodsAdd(goods);
    }

    @Override
    public void getGoodsUpd(Goods goods) {
        dao.getGoodsUpd(goods);
    }

    @Override
    public void getAdd(Goods goods) {
        dao.getAdd(goods);
    }

    @Override
    public void getUpd(Goods goods) {
        dao.getUpd(goods);
    }

    @Override
    public List<Goods> findGoodsBySupplierId(Integer supplierId) {
        return dao.findGoodsBySupplierId(supplierId);
    }

    @Override
    public List<Goods> getGoodsSupplierId(Integer supplierId) {
        return dao.getGoodsSupplierId(supplierId);
    }
}
